/**
 * Anderground project - Subway maps processor application.
 * Copyright (C) 2008-2009 Eldar Abusalimov, Ilya Shurigyn
 * 
 * This file is part of Anderground.
 * 
 * Anderground is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * Anderground is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with Anderground.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.anderground.core.model;

import java.io.Serializable;

import org.anderground.core.util.Bindable;
import org.anderground.core.util.Binder;
import org.anderground.core.util.BindingException;

/**
 * Transition class.
 * 
 * superclass for Transfer and Tunnel classes
 * 
 * @author Eldar Abusalimov (eldar.abusalimov@gmail.com)
 */
public abstract class Transition extends Bindable implements Serializable {

	/**
	 * @author Eldar Abusalimov (eldar.abusalimov@gmail.com)
	 * 
	 */
	public class Editor {

		/**
		 * private constructor
		 */
		protected Editor() {
		}

		/**
		 * @param time
		 *            the time to set
		 */
		public void setTime(int time) {
			Transition.this.time = time <= 0 ? DEFAULT_TIME : time;
		}

	}

	private static final long serialVersionUID = 5311660678426064693L;

	public static final int DEFAULT_TIME = 100;

	/** contains {@link Station} from which transition goes */
	private final Station fromStation;

	/** contains {@link Station} to which transition goes */
	private final Station toStation;

	/** represents transition duration (in seconds) */
	private int time;

	/**
	 * @param fromStation
	 * @param toStation
	 * @param time
	 *            - time is seconds between fromStation and toStation
	 */
	public Transition(Station fromStation, Station toStation, int time) {
		if (fromStation == null || toStation == null) {
			throw new IllegalArgumentException(
					"the ends of the transition cannot be null");
		}
		this.fromStation = fromStation;
		this.toStation = toStation;
		this.time = time <= 0 ? DEFAULT_TIME : time;
	}

	@Override
	protected void onBindTo(Binder binder) throws BindingException {
		fromStation.bindTo(binder);
		toStation.bindTo(binder);

		fromStation.bindWith(this);
		toStation.bindWith(this);
	}

	@Override
	protected void onUnbindFrom(Binder binder) throws BindingException {
		fromStation.unbindWith(this);
		toStation.unbindWith(this);
	}

	/**
	 * @return the {@link Station} from which this transition goes
	 */
	public Station getFromStation() {
		return fromStation;
	}

	/**
	 * @return the {@link Station} to which this transition goes
	 */
	public Station getToStation() {
		return toStation;
	}

	/**
	 * @return the time in seconds
	 */
	public int getTime() {
		return time;
	}

	abstract Editor getEditor();

	@Override
	public String toString() {
		return "from " + fromStation + " to " + toStation;
	}

	@Override
	public int hashCode() {
		if (isBound()) {
			return super.hashCode();
		} else {
			return fromStation.hashCode() + toStation.hashCode() + time * 31;
		}
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj) {
			return true;
		}

		// separation of the cases whether the transition is bound or not is a
		// consequence of using HashSets for storing transitions in ModelData.
		if (isBound()) {
			// we assume that in one model there could be two different
			// transitions with the same fields.
			// so we consider that the two different transition objects with the
			// same not-null binder represent different transitions.
			return false;
		}
		
		Transition another;
		if (obj instanceof Transition) {
			another = (Transition) obj;
		} else {
			return false;
		}

		if (!fromStation.equals(another.fromStation)) {
			return false;
		}
		if (!toStation.equals(another.toStation)) {
			return false;
		}
		if (time != another.time) {
			return false;
		}

		return true;
	}

}
